package com.nanjing.east.cloud.dao;

import com.nanjing.east.cloud.domain.PayOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class PaySpecification {

    @Autowired
    private PayOrderDAO payOrderDAO;

    public List<PayOrder> search(Date time, Integer payResult, String storeId) {
        List<PayOrder> payOrderList = new ArrayList<>();

        Specification<PayOrder> querySpecifi = new Specification<PayOrder>() {

            @Override
            public Predicate toPredicate(Root<PayOrder> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                //大于等于传入的值
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(Date.class), time));
                predicates.add(criteriaBuilder.equal(root.get("payResult").as(Integer.class), payResult));
                predicates.add(criteriaBuilder.equal(root.get("storeId").as(String.class), storeId));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        payOrderList = payOrderDAO.findAll(querySpecifi);
        return payOrderList;
    }

    public List<PayOrder> search(Integer payChannelId, Date time, Integer payResult) {
        List<PayOrder> payOrderList = new ArrayList<>();

        Specification<PayOrder> querySpecifi = new Specification<PayOrder>() {

            @Override
            public Predicate toPredicate(Root<PayOrder> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                //大于等于传入的值
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(Date.class), time));
                predicates.add(criteriaBuilder.or(criteriaBuilder.equal(root.get("payResult").as(Integer.class), payResult),
                        criteriaBuilder.equal(root.get("payResult").as(Integer.class), payResult)));
                predicates.add(criteriaBuilder.equal(root.get("payChannelId").as(Integer.class), payChannelId));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        payOrderList = payOrderDAO.findAll(querySpecifi);
        return payOrderList;
    }

    public List<PayOrder> search(Integer payChannelId, Date time, Integer payResult1, Integer payResult2) {
        List<PayOrder> payOrderList = new ArrayList<>();

        Specification<PayOrder> querySpecifi = new Specification<PayOrder>() {

            @Override
            public Predicate toPredicate(Root<PayOrder> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                //大于等于传入的值
                predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(Date.class), time));
                predicates.add(criteriaBuilder.or(criteriaBuilder.equal(root.get("payResult").as(Integer.class), payResult1),
                        criteriaBuilder.equal(root.get("payResult").as(Integer.class), payResult2)));
                predicates.add(criteriaBuilder.equal(root.get("payChannelId").as(Integer.class), payChannelId));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        payOrderList = payOrderDAO.findAll(querySpecifi);
        return payOrderList;
    }


}
